# $Id$
# Maintainer: Guillaume ALAUX <guillaume@archlinux.org>
# Contributor: Andrew Wright <andreww@photism.org>
# Contributor: Paul Mattal <paul@archlinux.org>
pkgbase=apache-ant
pkgname=('apache-ant' 'apache-ant-doc')
pkgver=1.9.7
pkgrel=1
arch=('any')
url='https://ant.apache.org/'
license=('APACHE')
makedepends=('java-environment' 'apache-ant' 'junit')
source=(https://www.apache.org/dist/ant/source/${pkgbase}-${pkgver}-src.tar.bz2{,.asc}
        apache-ant.sh
        apache-ant.csh
        apache-ant.install
        bin_ant
        ant.conf)
validpgpkeys=('CE8075A251547BEE249BC151A2115AE15F6B8B72') # Stefan Bodewig

sha256sums=('9e5c36759b81e0b16f2f1747631e81dc86fe904e45b413a6ca15d10b3282ab0b'
            'SKIP'
            '182b9212610790966d3a018d3cbab2e5327dd9fd11dc15dd1f377853216f5718'
            '919a3ab1acae1d0d190fe6e0034b00975caab40e55359a720bfccd098fe2d995'
            '3de451d0f963ba96e83f4e068a8325e3d4fc42b1e5c4818e9d7962ed4f5393b3'
            '29b443ae3af7e246b4ff0b5ec9f023ae95992db1a010a362e70ca7a65a6df461'
            'b86ce60f61cbd85a9faa61d698b0fc4607f1ff375cd15673aee57f053012eacb')

_ant_home=/usr/share/${pkgbase}

build() {
  cd "${srcdir}/${pkgbase}-${pkgver}"

  # ant needs HOME to download libs to $HOME/.m2/repository
  export ANT_OPTS="-Duser.home=${srcdir}"
  ant -Ddest=optional -f fetch.xml

  rm lib/optional/junit-4.11.jar
  ln -s /usr/share/java/junit.jar lib/optional/junit-4.11.jar
  rm lib/optional/hamcrest-core-1.3.jar
  ln -s /usr/share/java/hamcrest-core.jar lib/optional/hamcrest-core-1.3.jar

  ant dist
}

package_apache-ant() {
  pkgdesc='A java-based build tool'
  depends=('java-environment')
  optdepends=('junit: to use the junit tasks')
  install=apache-ant.install

  cd "${srcdir}/${pkgbase}-${pkgver}/${pkgbase}-${pkgver}"

  install -dm755 "${pkgdir}${_ant_home}"

  install -dm755 "${pkgdir}/etc/${pkgbase}"
  cp -Rp etc/* "${pkgdir}/etc/${pkgbase}"
  rm "${pkgdir}/etc/${pkgbase}/ant-bootstrap.jar"
  ln -s /etc/${pkgbase} "${pkgdir}${_ant_home}/etc"

  install -dm755 "${pkgdir}${_ant_home}/bin"
  find bin -type f -a ! -name \*.bat -a ! -name \*.cmd \
    -exec install -m755 {} "${pkgdir}${_ant_home}/bin" \;

  install -Dm755 "${srcdir}/bin_ant"  "${pkgdir}/usr/bin/ant"
  install -Dm644 "${srcdir}/ant.conf" "${pkgdir}/etc/apache-ant/ant.conf"

  install -dm755 "${pkgdir}/usr/share/java/${pkgbase}"
  install -m644 lib/*.jar "${pkgdir}/usr/share/java/${pkgbase}"
  ln -s /usr/share/java/${pkgbase} "${pkgdir}${_ant_home}/lib"

  # fix python2 path
  sed -i 's|/usr/bin/python|/usr/bin/python2|' "${pkgdir}${_ant_home}/bin/runant.py"

  # install profile.d script
  install -dm755 "${pkgdir}/etc/profile.d"
  install -m755 "${srcdir}"/apache-ant.{csh,sh} "${pkgdir}/etc/profile.d/"

  # License says NOTICE file should be redistributed for derivative works
  cd "${srcdir}/${pkgbase}-${pkgver}"
  install -dm755 "${pkgdir}/usr/share/licenses/${pkgbase}"
  install -m644 LICENSE NOTICE "${pkgdir}/usr/share/licenses/${pkgbase}"
}

package_apache-ant-doc() {
  pkgdesc='Apache Ant build tool documentation'

  install -dm755 "${pkgdir}/usr/share/doc/${pkgbase}"
  cp -r "${srcdir}"/${pkgbase}-${pkgver}/${pkgbase}-${pkgver}/manual/* \
    "${pkgdir}/usr/share/doc/${pkgbase}/"
}
